package top.ruanlitao.shortlink.project.initialize;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.ruanlitao.shortlink.project.common.constant.RedisKeyConstant;

/**
 * 初始化短链接监控消息队列消费者组
 */
@ConditionalOnProperty(prefix = "short-link.message-queue", name = "type", havingValue = "redis")
@Component
@RequiredArgsConstructor
public class ShortLinkStatsStreamInitializeTask implements InitializingBean {

    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public void afterPropertiesSet() {
        Boolean hasKey = stringRedisTemplate.hasKey(RedisKeyConstant.STATS_STREAM_TOPIC_KEY);
        if (!hasKey) {
            stringRedisTemplate.opsForStream().createGroup(RedisKeyConstant.STATS_STREAM_TOPIC_KEY, RedisKeyConstant.STATS_STREAM_GROUP_KEY);
        }
    }
}